package com.youxin.cpcenter.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.youxin.busfoundation.bean.ResponseObject;
import com.youxin.busfoundation.bean.delegate.RequestDelegateBean;
import com.youxin.busfoundation.common.BusinessDataException;
import com.youxin.busfoundation.enums.DelegateType;
import com.youxin.busfoundation.enums.EnterprisePassStatus;
import com.youxin.busfoundation.enums.SystemType;
import com.youxin.busfoundation.utils.DateTimeBusUtils;
import com.youxin.cpcenter.dao.TbMemberDelegationInfoMapper;
import com.youxin.cpcenter.dao.TbMemberInfoMapper;
import com.youxin.cpcenter.model.TbMemberDelegationInfo;
import com.youxin.cpcenter.model.TbMemberInfo;
import com.youxin.cpcenter.service.DelegateService;
import com.youxin.cpcenter.utils.MemberRightsUtils;
import com.youxin.foundation.db.utils.KeyGenerationUtils;
import com.youxin.foundation.utils.StringUtils;
/**
 * 委托实现
 * @author yingde.cao
 *
 */
@Service("delegateService")
public class DelegateServiceImpl implements DelegateService{

	@Autowired
	private TbMemberDelegationInfoMapper tbMemberDelegationInfoMapper;
	@Autowired
	private TbMemberInfoMapper tbMemberInfoMapper;
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public ResponseObject sendMessage(RequestDelegateBean bean) {
		TbMemberInfo memberInfo=MemberRightsUtils.isMemberIdentifyAndPassed(tbMemberInfoMapper);		
		TbMemberDelegationInfo tbMemberDelegationInfo=new TbMemberDelegationInfo();
		tbMemberDelegationInfo.setId(KeyGenerationUtils.generateKey());
		tbMemberDelegationInfo.setTarget(StringUtils.isNullSetEmpty(bean.getTarget()));
		tbMemberDelegationInfo.setMemberUid(memberInfo.getUid());
		tbMemberDelegationInfo.setCompanyUid(memberInfo.getCompanyUid());
		tbMemberDelegationInfo.setPhone(memberInfo.getPhone());
		tbMemberDelegationInfo.setWx(memberInfo.getWx());
		tbMemberDelegationInfo.setDetail(bean.getMessage());
		tbMemberDelegationInfo.setIsImportant(bean.getIsImportmant());
		int time=DateTimeBusUtils.getTimeSecondsInt();
		tbMemberDelegationInfo.setAddTime(time);
		tbMemberDelegationInfo.setUpdateTime(time);
		tbMemberDelegationInfo.setAreaCode("+86");
		tbMemberDelegationInfo.setType(DelegateType.Person.getValue());
		tbMemberDelegationInfo.setSystemType(SystemType.CP.getValue());
		tbMemberDelegationInfo.setStatus(EnterprisePassStatus.UnPass.getValue());
		
		int rowEffected=tbMemberDelegationInfoMapper.insertSelective(tbMemberDelegationInfo);
		if(rowEffected!=1)
		{
			throw new BusinessDataException("发送委托异常");
		}
		return new ResponseObject();
	}

}
